package com.xjj.service.controller;

import com.xjj.entity.ResponseEntity;
import com.xjj.service.job.SchedulerJob;
import com.xjj.service.job.UpdateJobStatus;
import com.xjj.vo.UpdateJobStatusVo;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description 创建定时任务
 * @Author zhangjin
 * @Date 2022/10/21 9:52
 */
@RestController
@RequestMapping("/scheduler/admin")
public class MyController {
    @Autowired
    private Scheduler scheduler;

    @RequestMapping("/make")
    public String create() throws Exception {
        JobBuilder jobBuilder = JobBuilder.newJob(SchedulerJob.class).withIdentity(JobKey.jobKey("job1","group1"));
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("value","com.woniuxy.springbootquartz.test.MyJob.test");
        jobBuilder.setJobData(jobDataMap);
        JobDetail jobDetail = jobBuilder.build();

        DateFormat dateformat=DateFormat.getDateInstance(DateFormat.LONG);
        // 时间格式化
        dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 具体执行时机
        String d= "2022-10-21 10:33:30";
        Date date = dateformat.parse(d);

        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule().withMisfireHandlingInstructionDoNothing();

        //CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(TriggerKey.triggerKey("triggerKey")).withSchedule(cronScheduleBuilder).build();

        // 简单模式执行：指定时间点执行，只会执行一次
        SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(TriggerKey.triggerKey("trigger1","group1")).startAt(date).forJob("job1","group1").build();
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        return "创建成功";
    }

    /**
     * 指定时间让作业截止
     * @param updateJobStatusVo
     * @return
     * @throws Exception
     */
    @PostMapping("/updateJobStatus")
    public ResponseEntity<String> updateJobStatus(@RequestBody UpdateJobStatusVo updateJobStatusVo) throws Exception {
        JobBuilder jobBuilder = JobBuilder.newJob(UpdateJobStatus.class).withIdentity(JobKey.jobKey("job1","group1"));
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("id",updateJobStatusVo.getId());
        jobBuilder.setJobData(jobDataMap);
        JobDetail jobDetail = jobBuilder.build();

        DateFormat dateformat=DateFormat.getDateInstance(DateFormat.LONG);
        // 时间格式化
        dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 具体执行时机
        String d= updateJobStatusVo.getDate();
        Date date = dateformat.parse(d);

        //CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule().withMisfireHandlingInstructionDoNothing();

        //CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(TriggerKey.triggerKey("triggerKey")).withSchedule(cronScheduleBuilder).build();

        // 简单模式执行：指定时间点执行，只会执行一次
        SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity(TriggerKey.triggerKey("trigger1","group1")).startAt(date).forJob("job1","group1").build();
        scheduler.scheduleJob(jobDetail,simpleTrigger);
        return ResponseEntity.SUCCESS("ok","创建成功");
    }
}
